package com.zhr.tea.utils;

import com.alibaba.fastjson.JSONObject;
import com.zhr.tea.entity.CarGoodsBean;
import com.zhr.tea.entity.shouye.End;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

import static com.zhr.tea.api.CarService.CAR_SHOP_DEL_URL;
import static com.zhr.tea.api.CarService.CAR_SHOP_URL;

/**
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 * Created by 毛懿 on 2016/11/3 12:46.
 * 邮箱：1334964028@qq.com
 */

public class CarUtils {
    public static void addCar(End.ContentBean goods) {
        final List<CarGoodsBean.ContentBean> data = new ArrayList<>();
        RequestParams params = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject obj = new JSONObject();
        obj.put("phoneNum", IsLogin.user.getBody().getMemberAccount());
        params.applicationJson(obj);
        HttpRequest.post(CAR_SHOP_URL, params, new BaseHttpRequestCallback<CarGoodsBean>() {
            @Override
            protected void onSuccess(CarGoodsBean carGoodsBean) {
                super.onSuccess(carGoodsBean);
                data.addAll(carGoodsBean.getContent());
            }
        });
        //向服务器发送新的购物车商品信息

        StringBuffer typeIds = new StringBuffer("134,");
        StringBuffer goodsIds = new StringBuffer("3020375,");
        for (int i = data.size() - 1; i >= 0; i--) {//删除选中的goods
            for (int j = data.get(i).getGoodsList().size() - 1; j >= 0; j--) {
                typeIds.append(data.get(i).getGoodsList().get(j).getTypeId() + ",");
                goodsIds.append(data.get(i).getGoodsList().get(j).getGoodsId() + ",");
            }
        }
        String s1 = typeIds.substring(0, typeIds.length() - 1);
        String s2 = goodsIds.substring(0, goodsIds.length() - 1);
        //向服务器发送新的购物车商品id
        RequestParams p = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject o = new JSONObject();
        obj.put("beginId", "0");
        obj.put("typeIds", s1);
        obj.put("loadSize", 20);
        obj.put("goodsIds", s2);
        p.applicationJson(o);
        HttpRequest.post(CAR_SHOP_DEL_URL, p, new BaseHttpRequestCallback());
    }
}
